package com.example.bill.control;

import com.example.bill.biz.Billbizimpl;
import com.example.bill.pojo.Bill;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ControllerBill {
    @Autowired
    private Billbizimpl billbizimpl;
    @RequestMapping("info")
    public String info(@RequestParam("userName")String userName, HttpSession session){
        session.setAttribute("userName",userName);
        return "redirect:getallbill";
    }
    
    @GetMapping("getallbill")
    public String getall(@RequestParam(value = "pageno", defaultValue = "1") int pageno, Model model) {
        PageInfo<Bill> pageinfo = this.billbizimpl.getallbill(pageno);
        List<Bill> bill = this.billbizimpl.distinct();
        model.addAttribute("bill", bill);
        model.addAttribute("page", pageinfo);
        return "mainbill";
    }

    @GetMapping("getbill")
    public String getbill(int id, Model model) {
        Bill bill = this.billbizimpl.getbill(id);
        model.addAttribute("bill", bill);
        return "viewbill";
    }

    @GetMapping("inser")
    public String inser(Model model) {
         List<Bill> bill = this.billbizimpl.distinct();
        model.addAttribute("bill", bill);
        return "insertbill";
    }

    @PostMapping("insertbill")
    public String insertbill(Bill bill) {
        bill.setCreationDate(new Date());
        int insertbill = this.billbizimpl.insertbill(bill);
        System.out.println(insertbill);
        return "redirect:getallbill";
    }

    @PostMapping("getbysel")
    public String getbysel(Bill bill, Model model) {

        if (bill.getProductName() != "" || bill.getIsPayment() != 0 || bill.getProviderId() != 0) {
            List<Bill> list = this.billbizimpl.getbysel(bill);
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            for (int i = 0; i < list.size(); i++) {
                Date date = list.get(i).getCreationDate();
                String s = sd.format(date);
                list.get(i).setCreationDates(s);
            }

            List<Bill> bills = this.billbizimpl.distinct();
            model.addAttribute("bill", bills);
            model.addAttribute("list", list);
            return "sle";
        }
        return "redirect:getallbill";
    }

    @GetMapping("uppro")
    public String up(int id, Model model) {
        Bill getbill = this.billbizimpl.getbill(id);
        List<Bill> bill = this.billbizimpl.distinct();
        model.addAttribute("bill", bill);
        model.addAttribute("provider", getbill);
        return "updatebill";
    }

    @PostMapping("upbill")
    public String upbill(Bill bill) {
        this.billbizimpl.updatebill(bill);
        return "redirect:getallbill";
    }

    @GetMapping("delpro")
    public String delpro(int id) {
        this.billbizimpl.delbill(id);
        return "redirect:getallbill";
    }
}
